package com.module.basic.controller;

import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.module.basic.domain.BasicDevice;
import com.module.common.core.domain.Ztree;
import com.module.common.core.page.TableDataInfo;
import com.module.common.utils.ShiroUtils;
import com.module.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.module.common.annotation.Log;
import com.module.common.enums.BusinessType;
import com.module.basic.service.IBasicDeviceService;
import com.module.common.core.controller.BaseController;
import com.module.common.core.domain.AjaxResult;
import org.springframework.web.multipart.MultipartFile;

/**
 * 采集设备Controller
 * 
 * @author ruoyi
 * @date 2025-05-17
 */
@Controller
@RequestMapping("/basic/device")
public class BasicDeviceController extends BaseController
{
    private String prefix = "basic/device";

    @Autowired
    private IBasicDeviceService basicDeviceService;

    @RequiresPermissions("basic:device:view")
    @GetMapping()
    public String device()
    {
        return prefix + "/device";
    }

    /**
     * 查询采集设备列表
     */
    @RequiresPermissions("basic:device:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BasicDevice basicDevice)
    {
        startPage();
        List<BasicDevice> list = basicDeviceService.selectBasicDeviceList(basicDevice);
        return getDataTable(list);
    }

    /**
     * 导出设备信息资料
     */
    @RequiresPermissions("basic:device:export")
    @Log(title = "设备信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BasicDevice basicDevice)
    {
        List<BasicDevice> list = basicDeviceService.selectBasicDeviceList(basicDevice);
        ExcelUtil<BasicDevice> util = new ExcelUtil<BasicDevice>(BasicDevice.class);
        return util.exportExcel(list, "采集设备数据");
    }

    /**
     * 下载采集设备信息导入模板
     */
    @RequiresPermissions("basic:device:import")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<BasicDevice> util = new ExcelUtil<BasicDevice>(BasicDevice.class);
        return util.importTemplateExcel("采集设备数据");
    }

    /**
     * 导入采集设备信息列表
     */
    @RequiresPermissions("basic:device:import")
    @Log(title = "采集设备信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<BasicDevice> util = new ExcelUtil<BasicDevice>(BasicDevice.class);
        List<BasicDevice> list = util.importExcel(file.getInputStream());
        String message = basicDeviceService.importBasicDevice(list, updateSupport, ShiroUtils.getLoginName());
        return AjaxResult.success(message);
    }

    /**
     * 新增采集设备
     */
    @RequiresPermissions("basic:device:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存采集设备
     */
    @RequiresPermissions("basic:device:add")
    @Log(title = "采集设备", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BasicDevice basicDevice)
    {
        if(!basicDeviceService.checkDeviceImeiUnique(basicDevice)) {
            return error("新增采集设备" + basicDevice.getDeviceImei() + "已存在，新增失败");
        }
        basicDevice.setCreateBy(getLoginName());
        return toAjax(basicDeviceService.insertBasicDevice(basicDevice));
    }

    /**
     * 修改采集设备
     */
    @RequiresPermissions("basic:device:edit")
    @GetMapping("/edit/{deviceId}")
    public String edit(@PathVariable("deviceId") Long deviceId, ModelMap mmap)
    {
        BasicDevice basicDevice = basicDeviceService.selectBasicDeviceByDeviceId(deviceId);
        mmap.put("basicDevice", basicDevice);
        return prefix + "/edit";
    }

    /**
     * 修改保存采集设备
     */
    @RequiresPermissions("basic:device:edit")
    @Log(title = "采集设备", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BasicDevice basicDevice)
    {
        if(!basicDeviceService.checkDeviceImeiUnique(basicDevice)) {
            return error("修改采集设备" + basicDevice.getDeviceImei() + "已存在，新增失败");
        }
        basicDevice.setUpdateBy(getLoginName());
        return toAjax(basicDeviceService.updateBasicDevice(basicDevice));
    }

    /**
     * 删除采集设备
     */
    @RequiresPermissions("basic:device:remove")
    @Log(title = "采集设备", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(basicDeviceService.deleteBasicDeviceByDeviceIds(ids));
    }

    /**
     * 校验imei号是否唯一
     */
    @PostMapping("/checkDeviceImeiUnique")
    @ResponseBody
    public boolean checkDeviceImeiUnique(BasicDevice basicDevice)
    {
        return basicDeviceService.checkDeviceImeiUnique(basicDevice);
    }
}
